package com.kk.app.render;

import android.content.Context;
import android.opengl.GLES10;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.kk.app.render.gles.FullFrameRect;
import com.kk.app.render.gles.Texture2dProgram;


/**
 * @author wk
 * @date 2024/9/14 10:57
 * @describe
 */
public class MVVideoRender extends RenderViewBase {
    private final static String TAG = "[MVVideoRender]";

    private FullFrameRect mFullFrameRect;
    private int mScreenWidth = 1920;
    private int mScreenHeight = 1080;

    private int mLeft = 0;
    private int mTop = 0;
    private int mWidth = 1920;
    private int mHeight = 1080;

    private MVRenderData mMVRenderData;

    public MVVideoRender(MVRenderData data) {
        mMVRenderData = data;
    }

    @Override
    public void onSurfaceCreated(Context context) {
        mFullFrameRect = new FullFrameRect(new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
        Log.i(TAG, "onSurfaceCreated: ");
    }

    @Override
    public void onSurfaceChanged(int width, int height) {
        mScreenWidth = width;
        mScreenHeight = height;
        /**
         * 默认显示区域等于客户端区域
         */
        mWidth = width;
        mHeight = height;
        Log.i(TAG, "onSurfaceChanged: w:" + width + " h:" + height);
    }

    @Override
    public void draw(float[] matrix) {
        if (!mVisiable)
            return;


        if (mMVRenderData != null && mMVRenderData.getSurfaceTexture() != null) {
            mMVRenderData.getSurfaceTexture().getTransformMatrix(matrix);
            GLES10.glViewport(mLeft, mTop, mWidth, mHeight);
            mFullFrameRect.drawFrame(mMVRenderData.getTextureId(), matrix);
        }
    }

    @Override
    public void setWindowPos(int left, int top, int w, int h) {
        mLeft = left;
        mTop = top;
        mWidth = w;
        mHeight = h;
    }

    public void updateLayout(FrameLayout.LayoutParams layoutParams) {

        Log.i(TAG, "updateLayout: l:" + layoutParams.leftMargin + " t:" + layoutParams.topMargin + " w:" + layoutParams.width + " h:" + layoutParams.height + " sw:" + mScreenWidth + "  sh:" + mScreenHeight);
        if (layoutParams.width == ViewGroup.LayoutParams.MATCH_PARENT) {
            mWidth = mScreenWidth;
        } else {
            mWidth = layoutParams.width;
        }
        if (layoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT) {
            mHeight = mScreenHeight;
        } else {
            mHeight = layoutParams.height;
        }
//
        if (layoutParams.gravity == Gravity.CENTER) {
            mLeft = (mScreenWidth - mWidth) / 2;
            mTop = (mScreenHeight - mHeight) / 2;
        } else {
            mLeft = layoutParams.leftMargin;
            mTop = mScreenHeight - mHeight - layoutParams.topMargin;
        }

        Log.i(TAG, "updateLayout end: left:" + mLeft + "  top:" + mTop + "  width:" + mWidth + " height:" + mHeight + "  sw:" + mScreenWidth + " sh:" + mScreenHeight);
    }

}
